/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using OpenServers.Net.Common.Utils;

namespace OpenServers.Net.HttpServer.Response {
    /// <summary>
    /// Holds information about the HTTP response.
    /// </summary>
    public class HttpResponse {
        private long? _length;
        private Stream _dataStream;
        private HttpResponseCode _responseCode = HttpResponseCode.SC200;
        private readonly IDictionary<string, string> _headers = new Dictionary<string, string>();
        private string _contentType;

        /// <summary>
        /// The length of the returned data.
        /// </summary>
        public long? Length {
            get { return _length; }
            set { _length = value; }
        }

        /// <summary>
        /// the return data stream.
        /// </summary>
        public Stream DataStream {
            get { return _dataStream; }
            set { _dataStream = value; }
        }

        /// <summary>
        /// The response code sent to the user.
        /// </summary>
        public HttpResponseCode ResponseCode {
            get { return _responseCode; }
            set { _responseCode = value; }
        }

        private string ResponseCodeString {
            get {
                switch (ResponseCode) {
                    case HttpResponseCode.SC200:
                        return "OK";
                    default:
                        throw new NotImplementedException();
                }
            }
        }

        /// <summary>
        /// Header dictionary sent back to the user.
        /// </summary>
        public IDictionary<string, string> Headers {
            get { return _headers; }
        }

        /// <summary>
        /// The content type of the response.
        /// </summary>
        public string ContentType {
            get { return _contentType; }
            set { _contentType = value; }
        }

        /// <summary>
        /// Gets the response stream. This is usually a composite of the header stream and data stream.
        /// </summary>
        public Stream GetStream() {
            CompositeStream result = new CompositeStream();

            StringBuilder responseHeader = new StringBuilder();

            responseHeader.AppendLine(string.Format("HTTP/1.1 {0} {1}", (int)ResponseCode, ResponseCodeString));

            _length = DataStream.Length;
            if (_length != null) {
                Headers.Remove("Content-Length");
                Headers.Add("Content-Length", Length.ToString());
            }
            if (ContentType != null) {
                Headers.Remove("Content-Type");
                Headers.Add("Content-Type", ContentType);
            }
            foreach (KeyValuePair<string, string> header in Headers) {
                responseHeader.AppendLine(string.Format("{0}: {1}", header.Key, header.Value));
            }
            responseHeader.AppendLine();

            byte[] buffer = ASCIIEncoding.ASCII.GetBytes(responseHeader.ToString());
            result.Add(new MemoryStream(buffer));
            result.Add(DataStream);
            return result;
        }
    }
}
